package com.mxin.network

import com.mxin.common.GlobalApp
import com.mxin.network.factory.LiveDataCallAdapterFactory
import com.mxin.network.factory.LiveDataCallAdapterLogicListener
import okhttp3.Interceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 *  Created by ：2020/7/28
 *  author : Administrator
 *
 *  初始化步骤：
 *  1. 设置 baseUrl
 *  2. 设置 拦截器Interceptor  {@see setInterceptor }
 *  3. 设置 LiveDate的BaseResponse处理
 *
 */
class ServiceGeneral {


    companion object{

        var debug = GlobalApp.debug

        private lateinit var baseUrl:String
//        private lateinit var logicListener: LiveDataCallAdapterLogicListener<Any>

        @JvmStatic
        fun setBaseUrl(baseUrl:String){
            this.baseUrl = baseUrl
        }

        @JvmStatic
        fun setInterceptor(vararg interceptors : Interceptor){
            OkHttpFactory.setInterceptor(*interceptors)
        }

//        @JvmStatic
//        fun setAdapterLogicListener(logicListener: LiveDataCallAdapterLogicListener<Any>){
//            this.logicListener = logicListener
//        }

        private val builder by lazy { build(baseUrl) }

        @JvmStatic
        val httpClient by lazy { OkHttpFactory.httpClient.build() }


        private fun build(baseUrl:String):Retrofit.Builder{
            return Retrofit.Builder()
                .baseUrl(baseUrl)
//                .addCallAdapterFactory(LiveDataCallAdapterFactory(logicListener))
                .addConverterFactory(GsonConverterFactory.create())
        }

        @JvmStatic
        fun <T> create(clazz: Class<T>) : T {
            val retrofit = builder.client(httpClient).build()
            return retrofit.create(clazz)
        }

    }




}